package leetcode.weekly.week338;

import java.util.ArrayList;
import java.util.List;

//Solution2Test
public class Solution2 {

	public boolean primeSubOperation(int[] nums) {
		List<Integer> list = primes(1000);
		for (int i = 0, last = -1; i < nums.length; i++) {
			int num = nums[i];
			int j = list.size() - 1;
			while (j >= 0 && list.get(j) >= num) {
				j--;
			}
			if (j != -1) {
				while (j >= 0 && num - list.get(j) <= last) {
					j--;
				}
			}
			if (j != -1) {
				num -= list.get(j);
			}
			if (num <= last) {
				return false;
			}
			last = num;
		}
		return true;
	}

	public List<Integer> primes(int n) {
		boolean[] dp = new boolean[n];
		List<Integer> primes = new ArrayList<>();
		for (int i = 2; i < n; i++) {
			if (!dp[i]) {
				primes.add(i);
			}
			for (int j = 0; j < primes.size(); j++) {
				int prime = primes.get(j);
				if (prime * i >= n) {
					break;
				}
				dp[prime * i] = true;
				if (i % prime == 0) {
					break;
				}
			}
		}
		return primes;
	}

}
